//////////////////////////////////////////////////////////////////////////
//             Copyright 2014  All Rights Reserved                //
//////////////////////////////////////////////////////////////////////////
/* =======================================================================
 * 系统名   : 
 * 子系统名 : 
 * Class名  : TestTree.java
 * 概要     : 
 * 改版履历 : 
 * 版本                     日期                    作者名             备注
 * 1.0.0    2015年10月21日        郭恩洲          初次作成
 * ======================================================================= */
package common;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class TestTree {
	 
    public static void main(String[] args) {
         
        Bean up1= getBean(11806,1,11802);
        Bean up2= getBean(11797,1,11806);
        Bean up3= getBean(11798,1,11796);
        Bean up4= getBean(11799,1,11804);
        Bean up5= getBean(11796,1,0);
        Bean up6= getBean(11800,2,0);
        Bean up7= getBean(11801,2,11802);
        Bean up8= getBean(11804,2,11796);
        Bean up9= getBean(11803,2,11804);
        Bean up10= getBean(11802,3,0);
        Bean up11= getBean(11805,4,0);
         
        List<Bean> list = new ArrayList<Bean>();
        list.add(up1);
        list.add(up2);
        list.add(up3);
        list.add(up4);
        list.add(up5);
        list.add(up6);
        list.add(up7);
        list.add(up8);
        list.add(up9);
        list.add(up10);
        list.add(up11);
         
        List<Bean> result = BeanSort.sort(list);
        System.out.println(JSONObject.toJSONString(result));
      /*  for (Bean bean : result) {
           
        }*/
    }
     
    private static Bean getBean(int id, int sortId, int parentId) {
        Bean bean = new Bean();
        bean.setId(id);
        bean.setSortId(sortId);
        bean.setParentId(parentId);
         
        return bean;
    }
}
 class Bean {
    private int id;
    private int sortId;
    private int parentId;
    private List<Bean> children = new ArrayList<Bean>();
    //省略get/set
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getSortId() {
		return sortId;
	}
	public void setSortId(int sortId) {
		this.sortId = sortId;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public List<Bean> getChildren() {
		return children;
	}
	public void setChildren(List<Bean> children) {
		this.children = children;
	}
    
    
}

 class BeanSort {
     
    public static List<Bean> sort(List<Bean> beans) {
         
        List<Bean> result = new ArrayList<Bean>();
         
        for (Bean bean : beans) {
            if (bean.getParentId() == 0) {
                result.add(bean);
                sort(bean.getId(), beans, result.get(result.size() - 1), result);
            }
        }
         
        return result;
    }
     
    private static void sort(long id, List<Bean> beans, Bean target, List<Bean> result) {
        for (Bean bean : beans) {
            if (bean.getParentId() == id) {
                target.getChildren().add(bean);
            } else {
                //这里怎么获取更下一级的？
            }
        }
    }
}